package com.zmwx.subject.appliacion.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.zmwx.subject.appliacion.convert.SubjectLabelDTOConverter;
import com.zmwx.subject.appliacion.dto.SubjectLabelDTO;
import com.zmwx.subject.common.entity.Result;
import com.zmwx.subject.domain.entity.SubjectLabelBO;
import com.zmwx.subject.domain.service.SubjectLabelDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/subject/label")
@Slf4j
public class SubjectLabelController {
    /**
     * 新增标签
     * @param subjectLabelDTO
     * @return
     */
    @Resource
    private SubjectLabelDomainService subjectLabelDomainService;
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        if (log.isInfoEnabled()) {
            log.info("subjectCategoryDTO:{}", subjectLabelDTO);
        }
        try {
            Preconditions.checkNotNull(subjectLabelDTO, "subjectLabelDTO不能为空");
            Preconditions.checkNotNull(subjectLabelDTO.getLabelName(), "labelName不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertByToCategory(subjectLabelDTO);
            if (log.isInfoEnabled()) {
                log.info("subjectLabelBO:{}", JSON.toJSONString(subjectLabelBO));
            }
            boolean result = subjectLabelDomainService.add(subjectLabelBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("label-add.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());


        }
    }

    /**
     * 更新标签
     * @param subjectLabelDTO
     * @return
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        if (log.isInfoEnabled()) {
            log.info("subjectCategoryDTO:{}", subjectLabelDTO);
        }
        try {
            Preconditions.checkNotNull(subjectLabelDTO, "subjectLabelDTO不能为空");
            Preconditions.checkNotNull(subjectLabelDTO.getId(), "Id不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertByToCategory(subjectLabelDTO);
            if (log.isInfoEnabled()) {
                log.info("subjectLabelBO.update:{}", JSON.toJSONString(subjectLabelBO));
            }
            boolean result = subjectLabelDomainService.update(subjectLabelBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("label.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());

        }
    }

    /**
     * 删除标签
     * @param subjectLabelDTO
     * @return
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        if (log.isInfoEnabled()) {
            log.info("subjectCategoryDTO:{}", subjectLabelDTO);
        }
        try {
            Preconditions.checkNotNull(subjectLabelDTO, "subjectLabelDTO不能为空");
            Preconditions.checkNotNull(subjectLabelDTO.getId(), "Id不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertByToCategory(subjectLabelDTO);
            if (log.isInfoEnabled()) {
                log.info("subjectLabelBO:{}", JSON.toJSONString(subjectLabelBO));
            }
            boolean result = subjectLabelDomainService.delete(subjectLabelBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("label-add.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());

        }
    }
    /**
     * 查询分类下标签
     * @param subjectLabelDTO
     * @return
     */
    @PostMapping("/queryLabelByCategoryId")
    public Result<List<SubjectLabelDTO>> queryLabelByCategoryId(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        if (log.isInfoEnabled()) {
            log.info("subjectCategoryDTO:{}", JSON.toJSONString(subjectLabelDTO));
        }
        try {
            Preconditions.checkNotNull(subjectLabelDTO, "subjectLabelDTO不能为空");
            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(), "CategoryId不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertByToCategory(subjectLabelDTO);
            if (log.isInfoEnabled()) {
                log.info("subjectLabelBO.update:{}", JSON.toJSONString(subjectLabelBO));
            }
            List<SubjectLabelBO> resultBO = subjectLabelDomainService.queryLabelByCategoryId(subjectLabelBO);
            List<SubjectLabelDTO> result = SubjectLabelDTOConverter.INSTANCE.subjectLabelBOConvertToDTO(resultBO);

            return Result.ok(result);
        } catch (Exception e) {
            log.error("label.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());

        }
    }
}
